package com.zone.mybatis.binding;

import com.zone.mybatis.session.SqlSession;

import java.lang.reflect.Proxy;

/**
 * @author: zongzi
 * @description: TODO
 * @date: 2023/12/13
 */
public class MapperProxyFactory<T> {
    private final Class<T> mapperInterface;

    public MapperProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public Class<T> getMapper() {
        return this.mapperInterface;
    }


    @SuppressWarnings("unchecked")
    public T newInstance(SqlSession sqlSession) {
        final MapperProxy<T> mapperProxy = new MapperProxy<>(mapperInterface, sqlSession);
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(),
                new Class[]{mapperInterface},
                mapperProxy);
    }
}
